package com.yangle.frame.common.utils;

import com.yangle.frame.admin.entity.BaseEntity;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * Created by yangle on 2018/6/3.
 */
@Component
public class Request2Object {
        public  BaseEntity getEntity(Map<String, String[]> map,Class<BaseEntity> clazz) throws Exception{
        try{
            BaseEntity domain=clazz.newInstance();
        Field[] fields = clazz.getDeclaredFields();
        for(Field field:fields){
            String fieldName = field.getName();
            if(map.get(fieldName) != null){
                field.setAccessible(true);
//                if(field.getType().toString().endsWith("String")){
                field.set(domain,map.get(fieldName)[0]);
//                }
            }
        }
        return domain;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
